package com.dgshanger.sanhxiaofeisc.mvp.presenter

import com.dgshanger.sanhxiaofeisc.entity.NearbyItemEntity
import com.dgshanger.sanhxiaofeisc.mvp.contract.CategoryContract
import com.dgshanger.sanhxiaofeisc.mvp.model.CategoryModel
import com.xx.baseuilibrary.mvp.BaseMvpPresenter
import com.xx.baseutilslibrary.network.rx.XxBaseHttpObserver

/**
 * Created by JacobHHH on 2018/5/4.
 */
class CategoryPresenter : BaseMvpPresenter<CategoryContract.Model,CategoryContract.View>(), CategoryContract.Presenter{

    override fun loadData(refresh: Boolean, location_name: String?, sort_distance: String?,
                          user_longitude: String?, user_latitude: String?, page: String?,
                          sort_new: String?, sort_comment: String?, shop_name: String?,
                          shop_cate_id: String?) {

        getModel().loadData(location_name,sort_distance,user_longitude,user_latitude,page,sort_new,
                sort_comment,shop_name,shop_cate_id,object : XxBaseHttpObserver<List<NearbyItemEntity>>(){

            override fun onCompleted(msg: String?, entity: List<NearbyItemEntity>?) {
                if (refresh){
                    getView()?.setData(entity)
                }else{
                    getView()?.addData(entity)
                }
            }

            override fun onError(error: String?) {
                getView()?.showError(Throwable(error),refresh)
                getView()?.onDataFailure(error)
            }

            override fun onStart() {

            }

            override fun onFinish() {
               getView()?.dismissLoadingDialog()
            }

        })
    }

    override fun createModel(): CategoryContract.Model = CategoryModel()
}